package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * Action_Response类
 * 实现动作响应的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class Action_Response implements AxdrType {

    private byte[] dataCode = null;
    private Choice choice = Choice._ERR_NONE_SELECTED;
    private ActionResponseNormal actionResponseNormal = null;
    private ActionResponseNormalList actionResponseNormalList = null;
    private ActionThenGetResponseNormalList actionThenGetResponseNormalList = null;

    public enum Choice {
        _ERR_NONE_SELECTED(-1),
        ACTION_RESPONSE_NORMAL(1),
        ACTION_RESPONSE_NORMAL_LIST(2),
        ACTION_THEN_GET_RESPONSE_NORMAL_LIST(3);

        private final int value;

        Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Choice fromValue(int value) {
            for (Choice choice : Choice.values()) {
                if (choice.value == value) {
                    return choice;
                }
            }
            return _ERR_NONE_SELECTED;
        }
    }

    public Action_Response() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setActionResponseNormal(ActionResponseNormal actionResponseNormal) {
        this.resetChoices();
        this.choice = Choice.ACTION_RESPONSE_NORMAL;
        this.actionResponseNormal = actionResponseNormal;
    }

    public void setActionResponseNormalList(ActionResponseNormalList actionResponseNormalList) {
        this.resetChoices();
        this.choice = Choice.ACTION_RESPONSE_NORMAL_LIST;
        this.actionResponseNormalList = actionResponseNormalList;
    }

    public void setActionThenGetResponseNormalList(ActionThenGetResponseNormalList actionThenGetResponseNormalList) {
        this.resetChoices();
        this.choice = Choice.ACTION_THEN_GET_RESPONSE_NORMAL_LIST;
        this.actionThenGetResponseNormalList = actionThenGetResponseNormalList;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;
        com.dl698.asn1.axdr.AxdrEnum choosen = new com.dl698.asn1.axdr.AxdrEnum();
        choosen.setConst();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = Choice.fromValue((int) choosen.getValue());

        if (this.choice == Choice.ACTION_RESPONSE_NORMAL) {
            this.actionResponseNormal = new ActionResponseNormal();
            codeLength += this.actionResponseNormal.decode(input);
            return codeLength;
        } else if (this.choice == Choice.ACTION_RESPONSE_NORMAL_LIST) {
            this.actionResponseNormalList = new ActionResponseNormalList();
            codeLength += this.actionResponseNormalList.decode(input);
            return codeLength;
        } else if (this.choice == Choice.ACTION_THEN_GET_RESPONSE_NORMAL_LIST) {
            this.actionThenGetResponseNormalList = new ActionThenGetResponseNormalList();
            codeLength += this.actionThenGetResponseNormalList.decode(input);
            return codeLength;
        }
        throw new RuntimeException("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        if (this.dataCode != null) {
            try {
                for (int i = this.dataCode.length - 1; i >= 0; i--) {
                    output.write(this.dataCode[i]);
                }
            } catch (java.io.IOException e) {
                throw new RuntimeException("Error writing dataCode to output stream", e);
            }
            return this.dataCode.length;
        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new RuntimeException("Error encoding AxdrChoice: No item in choice was selected.");
        }

        int codeLength = 0;

        if (this.choice == Choice.ACTION_RESPONSE_NORMAL) {
            if (this.actionResponseNormal != null) {
                codeLength += this.actionResponseNormal.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("actionResponseNormal is null");
            }
        } else if (this.choice == Choice.ACTION_RESPONSE_NORMAL_LIST) {
            if (this.actionResponseNormalList != null) {
                codeLength += this.actionResponseNormalList.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(2);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("actionResponseNormalList is null");
            }
        } else if (this.choice == Choice.ACTION_THEN_GET_RESPONSE_NORMAL_LIST) {
            if (this.actionThenGetResponseNormalList != null) {
                codeLength += this.actionThenGetResponseNormalList.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(3);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("actionThenGetResponseNormalList is null");
            }
        }

        throw new RuntimeException("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            encode(revOStream);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Error encoding Action_Response", e);
        }
        this.dataCode = revOStream.getArray();
    }

    public Choice getChoiceIndex() {
        return this.choice;
    }

    public ActionResponseNormal getActionResponseNormal() {
        return this.actionResponseNormal;
    }

    public ActionResponseNormalList getActionResponseNormalList() {
        return this.actionResponseNormalList;
    }

    public ActionThenGetResponseNormalList getActionThenGetResponseNormalList() {
        return this.actionThenGetResponseNormalList;
    }

    public void resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.actionResponseNormal = null;
        this.actionResponseNormalList = null;
        this.actionThenGetResponseNormalList = null;
    }

    @Override
    public String toString() {
        if (this.choice == Choice.ACTION_RESPONSE_NORMAL) {
            if (this.actionResponseNormal != null) {
                return "choice: {action_response_normal: " + this.actionResponseNormal + "}";
            } else {
                return "choice is action_response_normal but actionResponseNormal is null";
            }
        } else if (this.choice == Choice.ACTION_RESPONSE_NORMAL_LIST) {
            if (this.actionResponseNormalList != null) {
                return "choice: {action_response_normal_list: " + this.actionResponseNormalList + "}";
            } else {
                return "choice is action_response_normal_list but actionResponseNormalList is null";
            }
        } else if (this.choice == Choice.ACTION_THEN_GET_RESPONSE_NORMAL_LIST) {
            if (this.actionThenGetResponseNormalList != null) {
                return "choice: {action_then_get_response_normal_list: " + this.actionThenGetResponseNormalList + "}";
            } else {
                return "choice is action_then_get_response_normal_list but actionThenGetResponseNormalList is null";
            }
        } else {
            return "unknown";
        }
    }
}